package org.hybre.seed.core.action;

import java.util.List;

import org.hybre.seed.core.model.User;
import org.hybre.seed.core.service.UserManager;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class UserAction implements Preparable {
    private UserManager service;
    private List<User> persons;
    private User person;
    private Integer id;

    public UserAction(UserManager service) {
        this.service = service;
    }

    public String execute() {
        this.persons = service.findAll();
        return Action.SUCCESS;
    }

    public String save() {
        this.service.save(person);
        this.person = new User();
        return execute();
    }

    public String remove() {
        service.remove(id);
        return execute();
    }

    public List<User> getPersons() {
        return persons;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            person = service.find(id);
    }

    public User getPerson() {
        return person;
    }

    public void setPerson(User person) {
        this.person = person;
    }
}